#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>

#define high 20
#define width 40
#define enemynumber 5

int canvas[high][width]={0};
static int speed = 10;
int position_x,position_y;
int bullet_x,bullet_y;
int enemy_x[enemynumber],enemy_y[enemynumber];
int score;
int bulletwidth;

void gotoxy(int x,int y)//类似于清屏函数，光标移动到原点位置进行重画
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle,pos);
}

void startup() {
	position_x=high-1;
	position_y=width/4;
	canvas[position_x][position_y]=1;

	int k;
	for (k=0;k<enemynumber;k++) {
		enemy_x[k]=rand()%3;
		enemy_y[k]=rand()%width;
		canvas[enemy_x[k]][enemy_y[k]]=3;
	}
	bulletwidth=0;
	score=0;
}

void show() {
	gotoxy(0,0);
	int i,j;
	for (i=0;i<high;i++)
	{
		for (j=0;j<width;j++)
		{
			if (canvas[i][j]==1)
				printf("*");
			else if (canvas[i][j]==2)
				printf("|");
			else if (canvas[i][j]==3)
				printf("@");
			else
				printf(" ");
		}
		printf("\n");
	}
	printf("得分:%d",score);
}

void updateWithoutInput() {
	int i,j,k;
	for (i=0;i<high;i++)
	{
		for (j=0;j<width;j++)
		{
			if (canvas[i][j]==2)
			{
				for (k=0;k<enemynumber;k++)
				{
					if (i==enemy_x[k]&&j==enemy_y[k])
					{
						canvas[enemy_x[k]][enemy_y[k]]=0;
						enemy_x[k]=0;
						enemy_y[k]=rand()%(width-3)+1;
						canvas[enemy_x[k]][enemy_y[k]]=3;
						score++;
						if (score%2==0)
							speed--;
						if (score%5==0)
							bulletwidth+=2;
					}
				}
				
				canvas[i][j]=0;
				if (i>0)
					canvas[i-1][j]=2;
			}
			if (canvas[i][j]==1) {
				for (k=0;k<enemynumber;k++)
				if (i==enemy_x[k]&&j==enemy_y[k]) {
					printf("Game Over");
					system("pause");
					
					exit(0);
				}
			}
		}
	}
	for (k=0;k<enemynumber;k++)
	{
		if (enemy_x[k]>=high)
		{
			canvas[enemy_x[k]][enemy_y[k]]=0;
			enemy_x[k]=0;
			enemy_y[k]=rand()%(width-3)+1;
			canvas[enemy_x[k]][enemy_y[k]]=3;
			score--;
		}
	}

	
	
	if (speed<10)
		speed++;
	if (speed==10)
	{

	for (k=0;k<enemynumber;k++)
	{
		canvas[enemy_x[k]][enemy_y[k]]=0;
		enemy_x[k]++;
		canvas[enemy_x[k]][enemy_y[k]]=3;
	}
		speed=0;
	}
	
}

void updateWithInput() {
	char input;

	if (kbhit())
	{
		input=getch();
		if (input == 'a' && position_y>0) 
		{
			canvas[position_x][position_y] = 0;
			position_y--;  // 位置左移
			canvas[position_x][position_y] = 1;
		}
		else if (input == 'd' && position_y<width-1)
		{
			canvas[position_x][position_y] = 0;
			position_y++;  // 位置右移
			canvas[position_x][position_y] = 1;
		}
		else if (input == 'w')
		{
			canvas[position_x][position_y] = 0;
			position_x--;  // 位置上移
			canvas[position_x][position_y] = 1;
		}
		else if (input == 's')
		{
			canvas[position_x][position_y] = 0;
			position_x++;  // 位置下移
			canvas[position_x][position_y] = 1;
		}
		else if (input==' ')
		{
			int left,right;
			left=position_y-bulletwidth;
			right=position_y+bulletwidth;
			if (left<0)
				left = 0;
			if (right>width-1)
				right = width-1;
			int y;
			for (y=left;y<=right;y++)
				canvas[position_x-1][y] = 2;
		}
	}
}

int main() {
	startup();
	while(1) {
		show();
		updateWithInput();
		updateWithoutInput();
	}

return 0;
}